//
//  TopViewController.m
//  HWSGW
//
//  Created by avachen on 15/10/31.
//  Copyright © 2015年 luxuanrui. All rights reserved.
//

#import "TopViewController.h"
#import "Movie.h"
#import "HWDataService.h"
#import "TopCell.h"

@interface TopViewController ()

@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
}
- (void)requestData{
    NSDictionary *jsonDic = [HWDataService requestData:top250];
    NSArray *array = [jsonDic objectForKey:@"subjects"];
    
    _data = [[NSMutableArray alloc] initWithCapacity:array.count];
    
    //遍历解析json数据所取得的数组
    for (NSDictionary *subject in array) {
        
        //创建movie对象
        Movie *movie = [[Movie alloc] init];
        //把解析出来的数据存到model对象里
        movie.average = [[subject objectForKey:@"rating"] objectForKey:@"average"];
        movie.collect_count = [subject objectForKey:@"collect_count"];
        movie.images = [subject objectForKey:@"images"];
        movie.movieID = [subject objectForKey:@"id"];
        movie.title = [subject objectForKey:@"title"];
        movie.subtype = [subject objectForKey:@"subtype"];
        movie.year = [subject objectForKey:@"year"];
        movie.original_title = [subject objectForKey:@"original_title"];
        
        //将movie对象存到数组中
        [_data addObject:movie];
        
    }
    
    [_collectionView reloadData];

}
#pragma mark - UICollectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _data.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TopCell" forIndexPath:indexPath];
    cell.movie = self.data[indexPath.row];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
